from typing import List


class Solution:
    def shortestToChar(self, s: str, c: str) -> List[int]:
        '''
        左右两次遍历即可
        '''
        ans = [0] * len(s)
        cIndex = - len(s)
        for i, ch in enumerate(s):
            if ch == c:
                cIndex = i
            ans[i] = i - cIndex
        cIndex = 2 * len(s)
        for i in range(len(s) - 1, -1, -1):
            if s[i] == c:
                cIndex = i
            ans[i] = min(ans[i], cIndex - i)
        return ans

    def shortestToChar2(self, s: str, c: str) -> List[int]:
        '''
        两行代码，比上边的慢
        '''
        c_pos = [i for i in range(len(s)) if c == s[i]] # 找到所有c的位置
        return([min(abs(x-i) for i in c_pos) for x in range(len(s))]) # 遍历所有字符，每个字符都要遍历c_pos找最近的位置